package com.bys.systemclock.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bys.systemclock.R;

import java.util.stream.Stream;

/**
 * 选择 闹钟提醒的时机
 */
public class SelectRemindCyclePopup implements View.OnClickListener {
    private static final String TAG = "bys";
    private Context context;
//    选择闹钟提醒的弹窗
    private PopupWindow popupWindow;
//    文本框
    private TextView tv_monday,tv_tuesday,tv_wednesday,tv_thursday,tv_friday,tv_saturday,tv_sunday,tv_once,tv_everyday,tv_define;
    private SelectReindCyclePopupListener selectReindCyclePopupListener;

    public SelectRemindCyclePopup(Context context) {
        this.context = context;
//        弹出窗口控件，可以用来显示任意View，而且会浮动在当前activity的顶部
         popupWindow = new PopupWindow();
         popupWindow.setBackgroundDrawable(new BitmapDrawable());
         popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
         popupWindow.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
         popupWindow.setTouchable(true);
//        设置焦点，PopupWindow弹出后，所有的触屏和物理按键都由PopupWindows 处理
         popupWindow.setFocusable(true);
//         设置动画效果
         popupWindow.setAnimationStyle(R.style.animBottom);
         popupWindow.setContentView(initViews());
         popupWindow.getContentView().setOnTouchListener(new View.OnTouchListener() {
             @Override
             public boolean onTouch(View v, MotionEvent event) {
                 popupWindow.setFocusable(false);
                 return true;
             }
         });

    }

    private View initViews() {
        View view = LayoutInflater.from(context).inflate(R.layout.select_remind_cycle_popup_window, null);
        tv_everyday = view.findViewById(R.id.tv_drug_cycle_0);
        tv_monday = view.findViewById(R.id.tv_drug_cycle_1);
        tv_tuesday = view.findViewById(R.id.tv_drug_cycle_2);
        tv_wednesday = view.findViewById(R.id.tv_drug_cycle_3);
        tv_thursday = view.findViewById(R.id.tv_drug_cycle_4);
        tv_friday = view.findViewById(R.id.tv_drug_cycle_5);
        tv_saturday = view.findViewById(R.id.tv_drug_cycle_6);
        tv_sunday = view.findViewById(R.id.tv_drug_cycle_7);
        tv_once = view.findViewById(R.id.tv_drug_cycle_once);
        tv_define = view.findViewById(R.id.tv_drug_cycle_sure);
        Stream.of(tv_everyday,tv_monday,tv_tuesday,tv_wednesday,tv_thursday,tv_friday,tv_saturday,tv_sunday,tv_once,tv_define).forEach( v -> v.setOnClickListener(this));
        return view;
    }

    @Override
    public void onClick(View v) {
        Drawable nav_right = context.getResources().getDrawable(R.drawable.cycle_check);
        nav_right.setBounds(0,0,nav_right.getMinimumWidth(),nav_right.getMinimumHeight());
        switch (v.getId()){
            case R.id.tv_drug_cycle_once:
                Log.e(TAG, "onClick: 点击了指只响一次" );
                selectReindCyclePopupListener.obtainMessage(9,"");
                break;
            case R.id.tv_drug_cycle_0:
                selectReindCyclePopupListener.obtainMessage(8,"");
                break;
            case R.id.tv_drug_cycle_1:
//                如果 tv_monday 文本框右边的 图片为空的话
                putComponentRightDrawable(nav_right,0,tv_monday);
                break;
            case R.id.tv_drug_cycle_2:
                putComponentRightDrawable(nav_right,1,tv_tuesday);
                break;
            case R.id.tv_drug_cycle_3:
                putComponentRightDrawable(nav_right,2,tv_wednesday);
                break;
            case R.id.tv_drug_cycle_4:
                putComponentRightDrawable(nav_right,3,tv_thursday);
                break;
            case R.id.tv_drug_cycle_5:
                putComponentRightDrawable(nav_right,4,tv_friday);
                break;
            case R.id.tv_drug_cycle_6:
                putComponentRightDrawable(nav_right,5,tv_saturday);
                break;
            case R.id.tv_drug_cycle_7:
                putComponentRightDrawable(nav_right,6,tv_sunday);
                break;
            case R.id.tv_drug_cycle_sure:
                int remind  = ((tv_monday.getCompoundDrawables()[2] == null) ? 0 : 1)
                        +  ((tv_tuesday.getCompoundDrawables()[2] == null) ? 0 : 1) * 2
                        +  ((tv_wednesday.getCompoundDrawables()[2] == null) ? 0 : 1) * 4
                        +  ((tv_thursday.getCompoundDrawables()[2] == null) ? 0 : 1) * 8
                        +  ((tv_friday.getCompoundDrawables()[2] == null) ? 0 : 1) * 16
                        +  ((tv_saturday.getCompoundDrawables()[2] == null) ? 0 : 1) * 32
                        +  ((tv_sunday.getCompoundDrawables()[2] == null) ? 0 : 1) * 64;
                selectReindCyclePopupListener.obtainMessage(7,String.valueOf(remind));
                dismiss();
                break;
            default:
                break;
        }
    }

    private void putComponentRightDrawable(Drawable nav_right,int flag,TextView v) {
        if (v.getCompoundDrawables()[2] == null)
            v.setCompoundDrawables(null,null, nav_right,null);
        else
            v.setCompoundDrawables(null,null,null,null);
        selectReindCyclePopupListener.obtainMessage(flag,"");
    }

    public interface SelectReindCyclePopupListener{
        /**
         * 获取消息
         * @param flag
         * @param ret
         */
        void obtainMessage(int flag,String ret);
    }

    /**
     * 选择 提醒时机 监听器
     * @param l
     */
    public void setOnSelectRemindCyclePopupListener(SelectReindCyclePopupListener l){
        this.selectReindCyclePopupListener = l;
    }

    /**
     * 消除悬浮框
     */
    public void dismiss(){
        if(popupWindow != null && popupWindow.isShowing())
            popupWindow.dismiss();
    }
    public void showPopup(View rootView){
//        第一个参数要将 popupWindows 放到View,第二个参数是位置，第三第四都是偏移值
        popupWindow.showAtLocation(rootView, Gravity.BOTTOM,0,0);
        }



}
